package com.cl.short_video_java.factory;

import com.cl.short_video_java.exception.WebException;
import com.cl.short_video_java.menum.OssServerType;
import com.cl.short_video_java.service.SysSettingService;
import com.cl.short_video_java.util.AliOssUtil;
import com.cl.short_video_java.util.FtpUtil;
import com.cl.short_video_java.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * @Author 付
 * @Date 2024-11-14 11:28
 * @Version 1.0
 * @description
 * @since 1.0
 */
@Slf4j
@Component
public class OssFactory {
	private AliOssUtil aliOssUtil;
	private SysSettingService sysSettingService;
	private FtpUtil ftpUtil;

	@Autowired
	OssFactory(AliOssUtil aliOssUtil,
			   SysSettingService sysSettingService,
			   FtpUtil ftpUtil) {
		this.aliOssUtil = aliOssUtil;
		this.sysSettingService = sysSettingService;
		this.ftpUtil = ftpUtil;
	}


	public String getOss(MultipartFile file) throws IOException {
		String type = sysSettingService.getSysSettingByKey("sys_oss_type");
		String path = "";
		Integer isOss = Integer.valueOf(sysSettingService.getSysSettingByKey("sys_is_oss"));
		if (isOss == 0) {
			// 云服务器上传
			path = ftpUtil.uploadFile(file);
			log.info("是否上传成功=======", path);
			if (StringUtils.isEmpty(path)) {
				throw new WebException("图片上传失败");
			}
			return path;
		}

		if (type.equals(OssServerType.HUAWEI.getValue())) {

		} else if (type.equals(OssServerType.ALIYUN.getValue())) {
			// 阿里云oss
			path = aliOssUtil.upload(file);
		} else if (type.equals(OssServerType.TENCENT.getValue())) {

		}
		return path;
	}
}
